﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lbb.User;

namespace Comsenz.UCenter.Client.API
{
    public class AspNetClientAPI : ClientAPIBase
    {
        #region Helpers
        protected static void SetP3PHeader()
        {
            if (HttpContext.Current != null && HttpContext.Current.Response != null)
            {
                HttpContext.Current.Response.AddHeader("P3P: CP", "CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
            }
        }
        #endregion Helpers

        #region ClientAPIBase
        protected override bool SetLogined(int uid, string username)
        {
            SetP3PHeader();
            return MemberUser.SetLoginStatus(username, false, true, string.Format("{0}_{1}@liubaobao.cn", username, uid));
        }

        protected override bool SetLogoff()
        {
            SetP3PHeader();
            MemberUser.Logoff();
            return true;
        }

        protected override bool DeleteUsers(IEnumerable<int> ids)
        {
            if (ids != null && ids.Count() > 0)
            {
                try
                {
                    using (var repository = RepositroyHelper.GetUCenterMemberRepository())
                    {
                        var ucMembers = repository.Find(ucUid: ids).ToList();
                        foreach (var userId in repository.Find(ucUid: ids).Select(d => d.UserId))
                        {
                            MemberUser.DeleteUser(userId);
                        }
                    }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        protected override bool ChangeUserName(string oldname, string newname)
        {
            var mUser = MemberUser.GetUser(oldname);
            if (mUser == null) return false;
            try
            {
                mUser = mUser.ChangeUserName(newname);
            }
            catch
            {
                return false;
            }
            return true;
        }
        #endregion ClientAPIBase
    }
}
